/**
 * Created with JetBrains PhpStorm.
 * User: vander
 * Date: 5/25/13
 * Time: 12:50 AM
 * To change this template use File | Settings | File Templates.
 */
    /*
    * Nhiem vu cua collection la quan ly book hien tai dang co tren may (local)
    * Cac thong tin bao gom coverimage, book name, path to book resource
    * Add hay remove mot book ra khoi collections
    * Luu tru lai thong tin tren local
    * */
var Collections = Class.create({
    initialize: function(){
        this.books = new Array();

    },
    load: function(o){
        //o --> collections json object
        var _this = this;

        $j.each(o.books, function(index, value){
            //bookId, resourcesPath, name, coverImage
            var bookInfo = new Object();

            $j.each(value, function(key, val){
                bookInfo[key] = val;
            });

            _this.books.push(bookInfo);
        });
        $j("#"+Static.collectionsList).empty();

        //Xu ly su kien

        $j("#book-options li.add-favorite").off("click").on("click", function(e){
            e.stopPropagation();
            var bookId = $j("#book-options").attr("bookId");
            console.log("Add Favorite " + bookId);
            Global.favorite.add(_this.getBookInfoById(bookId));
            $j("#book-options").popup("close");
        });
        $j("#book-options li.delete-book").off("click").on("click", function(e){
            e.stopPropagation();
            var bookId = $j("#book-options").attr("bookId");
            _this.remove(bookId);
            $j("#book-options").popup("close");

        });

        $j.each(_this.books, function(index, value){
            var liTag = new Element("li", {bookId: value.bookId});
            var img = $j("<img>").attr('src',Global.localPath + value.resourcesPath + value.coverImage).attr({width: "80", height: "80"});
            var aTag = $j("<a></a>").append(img).append(value.name);
            var splitaTag = $j("<a href='#book-options' data-rel='popup'>Setting</a>");

            $j(splitaTag).on("click", function(){
                $j("#book-options").attr("bookId", value.bookId);
            });

            //Click thi view Book
            $j(aTag).on("tap", function(){
                _this.viewBook(value.bookId);
            });

            $j(liTag).append(aTag).append(splitaTag).appendTo("#" + Static.collectionsList);
        });
        $j("#"+Static.collectionsList).listview("refresh");
    },
    add: function(bookInfo){

        var value = bookInfo;
        this.books.push(bookInfo);
        this.save();
        //Tao doi tuong listview moi
        var liTag = new Element("li", {bookId: value.bookId});
        var img = $j("<img>").attr('src',Global.localPath + value.resourcesPath + value.coverImage).attr({width: "80", height: "80"});
        console.log("file://" + Global.localPath + value.resourcesPath + value.coverImage)
        var aTag = $j("<a></a>").append(img).append(value.name);
        var splitaTag = $j("<a href='#book-options' data-rel='popup'>Setting</a>");



        //Click thi view Book
        $j(aTag).on("tap", function(){
            _this.viewBook(value.bookId);
        });

        $j(liTag).append(aTag).append(splitaTag).appendTo("#" + Static.collectionsList);

        $j("#"+Static.collectionsList).listview("refresh");
    },
    remove: function(bookId){
        for(var i = 0; i < this.books.length; i++){
            if(this.books[i].bookId == bookId){
                var liTag = $j("#"+Static.collectionsList + " li[bookId=" + bookId +"]");
                $j(liTag).remove();

                this.books[i] = null;
                this.books = this.books.compact();
                $j("#"+Static.collectionsList).listview("refresh");

                //Xoa data local
                window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
                    fileSystem.root.getDirectory(bookId, null, function(dirEntry){
                        dirEntry.removeRecursively(function(){
                            console.log("Delete Book " + bookId + " completed !");
                        }, fail);
                    }, fail);
                }, fail);

                function fail(){
                    console.log("FAIL to Delete folder");
                }

                this.save();
                break;
            }
            return false;
        }
    },
    viewBook: function(bookId, pageId){
        if(Global.currentBook != null){
            Global.currentBook.destroy();
        }
        MyTools.openTextPersistent(bookId+"/"+bookId+".json", function(data){
            Global.currentBook = new MyBook($j.evalJSON(data));

            if(pageId != undefined){
                Global.currentBook.navigate(pageId);
            }
        });
    },
    isInCollections: function(bookId){
        for(var i = 0; i < this.books.length; i++){
            if(this.books[i].bookId == bookId){
                return true;
            }

            return false;
        }
    },
    save: function(){
        MyTools.saveTextPersistent("collections.json", $j.toJSON({books: this.books}));
    },
    getBookInfoById: function(bookId){
        for(var i = 0; i < this.books.length; i++){
            if(this.books[i].bookId == bookId){
                return this.books[i];
            }
        }
        return null;
    }
});